﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using OurSite.Business.Category;
using OurSite.Business.Event;
using OurSite.Business.Helper;
using OurSite.Business.Picture;
using OurSite.Business.Repositories;
using OurSite.Web.Models;

namespace OurSite.Web.Controllers
{
    [HandleError]
    public class PhotosController : Controller
    {
        #region Protected Properties
        string TitlePrefix = "Photos & Videos ";
        string yearTitlePrefix = "Years we live";
        string peopleTitlePrefix = "People we see";
        string placesTitlePrefix = "Places we go";
        string categoriesTitlePrefix = "Events we remember";
        IEventRepository eventRepository = new EventRepository();
        ICategoryRepository categoryRepository = new CategoryRepository();
        IPictureRepository pictureRepository = new PictureRepository();
        IPeopleRepository peopleRepository = new PeopleRepository();
        #endregion

        [Authorize]
        public ActionResult Index()
        {
            SetData("Our Favorite Photos", string.Empty);

            Event evt = new Event();
            evt.Name = "Our Favorite Photos";
            evt.Pictures = pictureRepository.GetAllFavoritePictures().ToList();

            EventModel eventModel = new EventModel();
            eventModel.Event = evt;

            return View(eventModel);
        }

        [Authorize]
        public ActionResult Years()
        {
            SetData(yearTitlePrefix, string.Empty);

            IList<Event> events = new List<Event>();
            // Show by decade starting in 1990
            int startYear = 1990;
            int groupingIncrement = 10;
            int endYear = startYear + groupingIncrement;
            var years = eventRepository.GetDistinctYears();
            
            // create grouping Event
            Event evt = EventGrouping(startYear, endYear, "Years");
            foreach(int y in years)
            {
                if(y <= endYear)
                {
                    Event subevt = new Event();
                    subevt.Name = y.ToString();
                    evt.SubEvents.Add(subevt);
                }

                // need to increase by the increament
                if(y == endYear)
                {
                    startYear = endYear + 1;
                    endYear += groupingIncrement;
                    events.Add(evt);
                    evt = EventGrouping(startYear, endYear, "Years");
                }
            }

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = "year";
            eventsModel.numberToGroup = 6;
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult Year(int id)
        {
            SetData(yearTitlePrefix + " - " + id, string.Empty);

            var events = eventRepository.GetByYear(id);

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = "event";
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult People()
        {
            SetData(peopleTitlePrefix, string.Empty);

            IList<Event> events = new List<Event>();
            Event hardenfamilyEvents = new Event();
            hardenfamilyEvents.Name = "Harden/Morrison Family";
            hardenfamilyEvents.ListingPicture = pictureRepository.GetPeopleListingPicture(PeopleCategory.IsHardenListingImage);

            Event ryanfamilyEvents = new Event();
            ryanfamilyEvents.Name = "Ryan/Weber Family";
            ryanfamilyEvents.ListingPicture = pictureRepository.GetPeopleListingPicture(PeopleCategory.IsRyanListingImage);

            Event friendEvents = new Event();
            friendEvents.Name = "Friends";
            friendEvents.ListingPicture = pictureRepository.GetPeopleListingPicture(PeopleCategory.IsFriendListingImage);

            var people = peopleRepository.GetAllHumanPeople();
            
            foreach (People person in people)
            {
                bool isUsed = false;
                if(person.LastName.Contains("Harden") || person.LastName.Contains("Morrison")
                    || person.MaidenName.Contains("Harden") || person.MaidenName.Contains("Morrison")
                    || person.LastName.Contains("Erika"))
                {
                    Event evt = new Event();
                    evt.Name = person.Name;
                    hardenfamilyEvents.SubEvents.Add(evt);
                    isUsed = true;
                }
                if (person.LastName.Contains("Ryan") || person.LastName.Contains("Weber")
                    || person.MaidenName.Contains("Ryan") || person.MaidenName.Contains("Weber"))
                {
                    Event evt = new Event();
                    evt.Name = person.Name;
                    ryanfamilyEvents.SubEvents.Add(evt);
                    isUsed = true;
                }

                if(!isUsed)
                {
                    Event evt = new Event();
                    evt.Name = person.Name;
                    friendEvents.SubEvents.Add(evt);
                }
            }

            events.Add(hardenfamilyEvents);
            events.Add(ryanfamilyEvents);
            events.Add(friendEvents);

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = "person";
            eventsModel.numberToGroup = 10;
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult Person(string id)
        {
            string lookupName = HelperUtilities.ReverseURLFriendly(id);
            SetData(peopleTitlePrefix + " - " + lookupName, string.Empty);

            var person = peopleRepository.GetPeopleByName(lookupName);
            Event evt = new Event();
            evt.Name = person.Name;
            evt.Pictures = person.Pictures.ToList();
            evt.Videos = person.Videos.ToList();

            EventModel eventModel = new EventModel();
            eventModel.Event = evt;

            return View(eventModel);
        }

        [Authorize]
        public ActionResult Places(string id)
        {
            SetData(placesTitlePrefix, string.Empty);

            IList<Event> events = new List<Event>();
            IList<string> regions = new List<string>();

            if (string.IsNullOrEmpty(id))
            {
                regions = eventRepository.GetDistinctRegions(string.Empty);
            }
            else
            {
                string regionName = HelperUtilities.ReverseURLFriendly(id);
                regions.Add(regionName);
            }

            foreach (string region in regions)
            {
                Event evt = new Event();
                evt.Name = region;
                evt.ListingPicture = pictureRepository.GetRegionListingPicture(region);
                var subRegions = eventRepository.GetDistinctCountries(region, string.Empty);
                foreach (string subRegion in subRegions)
                {
                    Event subEvent = new Event();
                    subEvent.Name = subRegion;
                    evt.SubEvents.Add(subEvent);
                }
                events.Add(evt);
            }

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = "place/country";
            eventsModel.numberToGroup = 10;
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult Place(string locationType, string locationName )
        {
            // Events by country
            string lookupName = HelperUtilities.ReverseURLFriendly(locationName);
            SetData(placesTitlePrefix + " - " + lookupName, string.Empty);
            string urlPath = "event";
            IList<Event> events = new List<Event>();

            switch (locationType)
            {
                case "country":
                    events = eventRepository.GetEventsByCountry(lookupName);

                    if (events.Count >= 32)
                    {
                        var list = eventRepository.GetDistinctStates(lookupName, string.Empty);
                        events = (from evt in list select new Event { Name = evt, ListingPicture = pictureRepository.GetStateListingPicture(evt) }).ToList();
                        urlPath = "place/state";
                    }
                    break;
                case "state":
                    events = eventRepository.GetEventsByState(lookupName);
                    break;
                default:
                    break;
            }

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = urlPath;
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult Categories(string id)
        {
            SetData(categoriesTitlePrefix, string.Empty);

            IList<Event> events = new List<Event>();
            IList<Category> parentCategories = new List<Category>();
            
            if (string.IsNullOrEmpty(id))
                parentCategories = categoryRepository.GetAllParentCategories();
            else
            {
                string categoryName = HelperUtilities.ReverseURLFriendly(id);
                parentCategories.Add(categoryRepository.GetByName(categoryName));
            }

            foreach(Category parentCategory in parentCategories)
            {
                Event evt = new Event();
                evt.Name = parentCategory.Name;
                evt.ListingPicture = pictureRepository.GetCategoryListingPicture(parentCategory.Name);
 
                foreach(Category subCategory in parentCategory.SubCategories)
                {
                    Event subEvent = new Event();
                    subEvent.Name = subCategory.Name;
                    evt.SubEvents.Add(subEvent);
                }
                events.Add(evt);
            }

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = "category";
            eventsModel.numberToGroup = 10;
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult Category(string id)
        {
            string category = HelperUtilities.ReverseURLFriendly(id);
            SetData(categoriesTitlePrefix + " - " + category, string.Empty);

            var events = eventRepository.GetEventsBySubCategory(category);

            EventsModel eventsModel = new EventsModel();
            eventsModel.Events = events;
            eventsModel.URLPath = "event";
            return View(eventsModel);
        }

        [Authorize]
        public ActionResult Event(int eventCode, string eventName)
        {
            string lookupName = HelperUtilities.ReverseURLFriendly(eventName);
            SetData(lookupName, string.Empty);

            Event evt = eventRepository.GetByEventCode(eventCode);
            EventModel eventModel = new EventModel();
            eventModel.Event = evt;

            return View(eventModel);
        }

        [ChildActionOnly]
        public ActionResult PhotoMenu()
        {
            ViewData["MenuSelectedItem"] = TempData["MenuSelectedItem"];
            var years = eventRepository.GetDistinctYears();
            var ryanYears = eventRepository.GetDistinctYearsByFamily(1);
            var hardenYears = eventRepository.GetDistinctYearsByFamily(2);
            var regions = eventRepository.GetDistinctRegions(string.Empty);
            var parentCategories = categoryRepository.GetAllParentCategories();
            var people = peopleRepository.GetAllPeople();

            PhotoMenuModel menuModel = new PhotoMenuModel();
            menuModel.Years = years;
            menuModel.RyanYears = ryanYears;
            menuModel.HardenYears = hardenYears;
            menuModel.ParentCategories = parentCategories;
            menuModel.People = people;
            menuModel.Regions = regions;

            return View(menuModel);
        }

        protected Event EventGrouping(int start, int end, string name)
        {
            Event evt = new Event();
            evt.Name = name + " " + start + " - " + end;
            evt.ListingPicture = pictureRepository.GetDecadeListingPicture(start.ToString(), end.ToString());
            return evt;
        }

        protected void SetData(string additionalTitle, string menuSelect)
        {
            if (additionalTitle != string.Empty)
                ViewData["Title"] = TitlePrefix + " - " + additionalTitle;
            else
                ViewData["Title"] = TitlePrefix;

            ViewData["SectionTitle"] = additionalTitle;
            TempData["MenuSelectedItem"] = string.Empty;
        }
    }
}
